#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <error.h>
#include <ctype.h>
#define SERVER_IP "0,0,0,0"
#define PORT 8888
#define BUFFER_SIZE 64
#define CLIENT_IP "172.23.232.7"
int main()
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {
        perror("socket error");
        exit(-1);
    }
    struct sockaddr_in severAddr;
    memset(&severAddr, 0, sizeof(severAddr));
    socklen_t severAddrLen = sizeof(severAddr);
    severAddr.sin_family = AF_INET;
    severAddr.sin_port = htons(PORT);

    int ret = inet_pton(AF_INET, SERVER_IP, (void *)&severAddr.sin_addr.s_addr);
    if (ret == -1)
    {
        perror("inet_pton error");
    }
    ret = bind(sockfd, (struct sockaddr *)&severAddr, severAddrLen);
    if (ret == -1)
    {
        perror("bind error");
    }
    char revMassage[BUFFER_SIZE];
    memset(&revMassage, 0, sizeof(revMassage));

    char sendMassage[BUFFER_SIZE];
    memset(&sendMassage, 0, sizeof(sendMassage));
    strncpy(sendMassage, "hello", sizeof(sendMassage) - 1);
    struct sockaddr_in clientAddr;
    socklen_t clientAddrLen = sizeof(clientAddr);
    int readBytes = 0;
    int sendBytes = 0;
    while (1)
    {

        readBytes = recvfrom(sockfd, (void *)revMassage, sizeof(revMassage), 0, (struct sockaddr *)&clientAddr, &clientAddrLen);
        if (readBytes <= 0)
        {
            printf("readBytes=%d\n", readBytes);
            perror("recvfrom error");
            close(sockfd);
            exit(-1);
            break;
        }
        else
        {
            for (int idx = 0; idx < readBytes; idx++)

            {
#if 0
           
            {
                if (revMassage[idx] >= 'a' && revMassage[idx] <= 'z')
                {
                    revMassage[idx] = revMassage[idx] - 32;
                }
            }
#else
                revMassage[idx] = toupper(revMassage[idx]);
#endif
            }
        }

        printf("client message:%s\n", revMassage);
        sendBytes = sendto(sockfd, sendMassage, sizeof(sendMassage), 0, (struct sockaddr *)&clientAddr, clientAddrLen);
        if (sendBytes <= 0)
        {
            perror("sendto error");
            close(sockfd);
            exit(-1);
            break;
        }
        sleep(3);
    }
    close(sockfd);
}